package com.apexsoft.monitor.front.console.file.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.support.BaseFrontController;
import com.apexsoft.monitor.front.console.file.service.fileListService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller("FileController")
@RequestMapping("/console/file")
public class FileController  extends BaseFrontController {
    /**
     * 主机监控
     */
    @Autowired
    @Qualifier("fileListService")
    private fileListService fileListService = null;
    private static final Logger log = LogManager.getRootLogger();
    @RequestMapping(value = "/list")
    public String monitor(HttpServletRequest request, HttpServletResponse response, ModelMap modelMap) throws Exception {
        return "console/file/list";

    }


    @RequestMapping(value = "/fileList")
    public JSONArray fileList(HttpServletRequest request,HttpServletResponse response) throws Exception
    {

        String currentType=new String();
        String currentPath=new String();
        String currentUrl=new String();
        String currentShowFile=new String();
        JSONArray list=null;

        // 当前选中的节点类型
        currentType = request.getParameter("currentType");
        // 当前选中的中间件
        currentPath = request.getParameter("currentPath");
        //选中的链接
        currentUrl = request.getParameter("currentUrl");
        //是否展示文件，false则只展示文件夹
        currentShowFile= request.getParameter("currentShowFile");
//        showFile="false";
        //System.out.println("当前选中路径"+currentPath+"类型"+currentType+"url:"+currentUrl);

        list = fileListService.treeData(currentType,currentPath,currentUrl,currentShowFile);
        this.printJSON(response,list);
        return null;
    }

    @RequestMapping(value = "/ShowTextFile")
    public JSONObject ShowTextFile(HttpServletRequest request, HttpServletResponse response) throws Exception{
        JSONObject showText=new JSONObject();
        String currentUrl=new String();
        String currentPath=new String();

        currentPath=request.getParameter("currentPath");
        currentUrl=request.getParameter("currentUrl");

        showText=fileListService.showText(currentUrl,currentPath);
        this.printJSON(response,showText);
        return null;
    }

}

